package main

import (
	"encoding/json"
	"fmt"
	"path/filepath"
	"regexp"
	"strings"
)

type RemindType int
type RemindQueue struct {
	ID          string     `json:"id"`
	TodoId      int64      `json:"todoId"`
	FileId      int64      `json:"fileId"`
	UserId      int64      `json:"userId"`
	RemindAt    string     `json:"remindAt"`
	IsSetTime   bool       `json:"isSetTime"`
	PositionId  string     `json:"positionId"`
	Type        RemindType `json:"type"`
	Msg         string     `json:"msg"`
	RemindUsers []int64    `json:"remindUsers"` // 删除时需要通知的用户 id
}

func main() {
	jsonStr := "{\"id\":\"115\",\"todoId\":114,\"fileId\":5000022832,\"userId\":99197,\"remindAt\":\"2025-07-08T06:31:00.000Z\",\"isSetTime\":true,\"positionId\":\"\",\"type\":0,\"msg\":\"\",\"remindUsers\":null}"
	var remindQueue RemindQueue
	err := json.Unmarshal([]byte(jsonStr), &remindQueue)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(remindQueue)

	filename := `A.B\C/D:E*F?G"H<I>J|`
	clean := ReplaceInvalidCharsPreserveExt(filename)
	fmt.Println(clean)
}

// ReplaceInvalidCharsPreserveExt 替换非法字符为 _，保留扩展名
func ReplaceInvalidCharsPreserveExt(filename string) string {
	ext := filepath.Ext(filename)             // 提取扩展名（包括 .）
	name := strings.TrimSuffix(filename, ext) // 提取不带扩展名的部分

	// 替换非法字符，包括点
	invalidChars := regexp.MustCompile(`[<>:"/\\|?*\x00-\x1F.]`)
	safeName := invalidChars.ReplaceAllString(name, "_")
	safeName = regexp.MustCompile(`_+`).ReplaceAllString(safeName, "_")
	safeName = strings.Trim(safeName, "_")

	// 拼接回扩展名（如果原来有扩展名）
	if ext != "" {
		return safeName + ext
	}
	return safeName
}
